package main

func CheckCellStatus(board [][]int, i, j int) bool {
	isLive := false
	liveCellNum := 0
	if i-1 >= 0 && board[i-1][j] == 1 {
		liveCellNum++
	}

	if i+1 < len(board) && board[i+1][j] == 1 {
		liveCellNum++
	}

	if j-1 >= 0 && board[i][j-1] == 1 {
		liveCellNum++
	}

	if j+1 < len(board[0]) && board[i][j+1] == 1 {
		liveCellNum++
	}

	if i-1 >= 0 && j-1 >= 0 && board[i-1][j-1] == 1 {
		liveCellNum++
	}

	if i-1 >= 0 && j+1 < len(board[0]) && board[i-1][j+1] == 1 {
		liveCellNum++
	}

	if i+1 < len(board) && j-1 >= 0 && board[i+1][j-1] == 1 {
		liveCellNum++
	}

	if i+1 < len(board) && j+1 < len(board[0]) && board[i+1][j+1] == 1 {
		liveCellNum++
	}

	if board[i][j] == 1 {
		if liveCellNum == 2 || liveCellNum == 3 {
			isLive = true
		}
	} else {
		if liveCellNum == 3 {
			isLive = true
		}
	}

	return isLive
}

func gameOfLife(board [][]int) {
	result := make([][]int, len(board))

	for i := 0; i < len(result); i++ {
		temp := make([]int, len(board[0]))
		result[i] = temp
	}

	for i := 0; i < len(board); i++ {
		for j := 0; j < len(board[0]); j++ {
			isLive := CheckCellStatus(board, i, j)

			if isLive {
				result[i][j] = 1
			} else {
				result[i][j] = 0
			}
		}
	}

	for i := 0; i < len(board); i++ {
		for j := 0; j < len(board[0]); j++ {
			board[i][j] = result[i][j]
		}
	}
}
